package code.utils
{
	import code.ui.FilterUtil;
	
	import com.darwin.darwinDll.Dll;
	import com.darwin.darwinDll.core.DllItem;
	import com.darwin.darwinUI.controls.Label;
	import com.darwin.darwinUI.controls.LabelButton;
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.general.TextFieldFactory;
	
	import flash.display.DisplayObject;
	import flash.filters.ColorMatrixFilter;
	
	import main.AppFont;
	
	import module.main.view.ResourceConst;

	/**
	 *	UI界面按钮工具类,提供创建各类按钮的静态方法 
	 * @author Roland
	 * 
	 */
	public class ButtonUtil
	{
		/**
		 *	按钮风格-普通 
		 */
		public static const BUTTON_STYLE_NORMAL:String ="normalStyle";
		/**
		 *	 按钮风格-蓝
		 */
		public static const BUTTON_STYLE_BLUE:String = "blueStyle";
		
		public function ButtonUtil()
		{
		}
		
		
		/**
		 *	创建通用字体按钮 
		 * @param label 		文本label
		 * @param buttonWidth 按钮宽度
		 * @param buttonHeight 按钮长度
		 * @param buttonStyle 设置按钮样式 通过ButtonUtil中的静态常量定义  	默认值：ButtonUtil.BUTTON_STYLE_ORANGE
		 * @param scale 是否是缩放式		
		 * @param skin 按钮皮肤,不设置则为默认皮肤						默认值：ResourceConst.GENERAL_BUTTON
		 * @param widthAuto 宽度自适应
		 * @return 
		 * 
		 */
		public static function createGeneralButton(label:Label,buttonWidth:Number=90.5,buttonHeight:Number=32.9,__buttonStyle:String =null,scale:Boolean = true,__skin:String = null,widthAuto:Boolean = true):LabelButton
		{
			var buttonStyle:String 	= __buttonStyle || ButtonUtil.BUTTON_STYLE_NORMAL;
			var skin:String 		= __skin || ResourceConst.GENERAL_BUTTON;
			var $skin:UIComponent = new UIComponent;
			$skin.backgroundSkin = skin;
			
			var $button:LabelButton = new LabelButton("",$skin);
			$button.label = label;
			$button.label.x = (buttonWidth-$button.label.width)/2+2;
			$button.label.y = (buttonHeight-$button.label.height)/2+2;
			$button.width 			= buttonWidth;
			$button.height 			= buttonHeight;
			setButtonStyle(buttonStyle,$button,$skin);
//			$button.textField.width = $button.textField.textWidth;
//			$button.textField.height= $button.textField.textHeight;
//			$button.widthAuto = widthAuto;
//			if(widthAuto)
//			{
//				$button.padding = Math.abs(buttonWidth-$button.textField.textWidth)/2;
//				$button.textField.x = $button.padding;
//			}
//			else
//			{
//				$button.textField.x = (buttonWidth - $button.textField.textWidth)/3;
//			}
			return $button;
		}
		
		/**
		 *	设置按钮风格 
		 * @param style
		 * @param button
		 */
		public static function setButtonStyle(style:String,button:LabelButton,skin:DisplayObject):void
		{
			var $filterMatrix:Array = [];
			var $colorFilter:ColorMatrixFilter;
			switch(style)
			{
				case BUTTON_STYLE_NORMAL:
					skin.filters = [];
					button.textFilters = FilterUtil.createDSFilter(0xffffff,5,0.4,45);
					break;
				case BUTTON_STYLE_BLUE:
					$filterMatrix = [-2.4222016,3.2559296,0.286272,0,3.58,0.7137984,0.1199296,0.286272,0,3.58,0.7137984,3.2559296,-2.849728,0,3.57999999999999,0,0,0,1,0];
					$colorFilter = new ColorMatrixFilter($filterMatrix);
					skin.filters = [$colorFilter];
					button.textFilters = FilterUtil.createDSFilter(0xFFFFFF,5,0.4,45,1,1,1);
					break;
			}
		}
		
		/**
		 * 改造类的按钮 
		 * @return 
		 * 
		 */		
		public static function getReformBtn(subkey:String,labelStr:Label):LabelButton{
			var label:Label = labelStr;
			var skinClass:Class = Dll.getInstance().getSynPreloadRes("",subkey,DllItem.TYPE_SWF);
			var skin:DisplayObject 		=  new skinClass() as DisplayObject;
			var $skin:UIComponent = new UIComponent;
			$skin.width = 65;
			$skin.height = 65;
			$skin.addChild(skin);
			var $skinContainer:UIComponent= new UIComponent;
			$skinContainer.addChild($skin);
			$skinContainer.addChild(label);
			label.x = ($skin.width - label.width)/2;
			label.y = $skin.height - label.height;
			
			var $button:LabelButton = new LabelButton("",$skinContainer);
//			$button.label = label;
//			$button.label.x = ($skinContainer.width-$button.label.width)/2;
//			$button.label.y = 5;
			//			$button.label.y = ($skinContainer.height-$button.label.height)/2+2;
//			setButtonStyle(buttonStyle,$button,$skin);
			return $button;
		}
		
		/**
		 * 建造列表之建造按钮 
		 * @return 
		 * 
		 */
		public static function getBuildlistBuildBtn():LabelButton{
			var label:Label = TextFieldFactory.getInstance().getLabel("buildInterface_button_build");
			var buttonStyle:String 	= ButtonUtil.BUTTON_STYLE_BLUE;
			var skin:String 		= ResourceConst.GENERAL_BUTTON;
			var $skin:UIComponent = new UIComponent;
			$skin.width = 90;
			$skin.height = 33;
			$skin.backgroundSkin = skin;
			var $iconSkin:UIComponent = new UIComponent;
			$iconSkin.width = 42;
			$iconSkin.height= 35;
			$iconSkin.x=-14;
			$iconSkin.backgroundSkin = ResourceConst.OPERATE_PLAYER_BUILDING_ICON;
			var $skinContainer:UIComponent= new UIComponent;
			$skinContainer.addChild($skin);
			$skinContainer.addChild($iconSkin);
			
			var $button:LabelButton = new LabelButton("",$skinContainer);
			$button.label = label;
			$button.label.x = ($skinContainer.width-$button.label.width)/2;
			$button.label.y = 5;
//			$button.label.y = ($skinContainer.height-$button.label.height)/2+2;
			setButtonStyle(buttonStyle,$button,$skin);
			return $button;
		}
	}
}